package com.czxy.changgou3.service;

import com.czxy.changgou3.repository.SkuRepository;
import com.czxy.changgou3.vo.ReturnSku;
import com.czxy.changgou3.vo.SearchSku;
import com.czxy.changgou3.vo.SearchVo;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangmengxiang1@itcast.cn
 * @version 1.0
 * @date 2020/4/24
 */
@Service
public class SkuSearchService {

    @Resource
    private SkuRepository skuRepository;

    public Map search(SearchVo searchVo){
        //1 拼凑多条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //分类 /list/76
        boolQueryBuilder.must(QueryBuilders.termQuery("catId",searchVo.getCatId()));

        //品牌id
        if (searchVo.getBrandId()!= null){
            boolQueryBuilder.must(QueryBuilders.termQuery("brandId",searchVo.getBrandId()));
        }

        //关键字
        if (StringUtils.isNotBlank(searchVo.getKeyword())){
            boolQueryBuilder.must(QueryBuilders.matchQuery("all",searchVo.getKeyword()));
        }

        // 规范查询 map.key = "机身颜色" map.vlaue = "金色".
        if (searchVo.getSpecList()!=null){
            for (String key : searchVo.getSpecList().keySet()) {
                String value = searchVo.getSpecList().get(key);
                boolQueryBuilder.must(QueryBuilders.termQuery("specs."+key+".keyword",value));
            }
        }

        //价格区间
        if (searchVo.getMinPrice()!=null){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("price").gte(searchVo.getMinPrice()));
        }
        if (searchVo.getMaxPrice()!=null){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("price").lte(searchVo.getMaxPrice()));
        }


        //2 查询条件构建器
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();

        //2.1 查询条件
        queryBuilder.withQuery(boolQueryBuilder);

        //分页
        queryBuilder.withPageable(PageRequest.of( searchVo.getPageNum() - 1 , searchVo.getPageSize()));
        //排序
        HashMap<String, String> map = new HashMap<>();
        map.put("xl","sellerCount"); //销量
        map.put("jg","price"); //价格
        map.put("pl","commentCount"); //评论
        map.put("sj","onSaleTime"); //时间

        String propName = map.get(searchVo.getSortBy());

        if (StringUtils.isNotBlank(propName)){
            FieldSortBuilder fieldSortBuilder = SortBuilders.fieldSort(propName);
            if ("desc".equals(searchVo.getSortWay())){
                fieldSortBuilder.order(SortOrder.DESC);
            }else {
                fieldSortBuilder.order(SortOrder.ASC);
            }
            queryBuilder.withSort(fieldSortBuilder);
        }

        //查询
        Page<SearchSku> page = skuRepository.search(queryBuilder.build());

        //数据处理
        long count = page.getTotalElements();

        //查询
        List<ReturnSku> list = new ArrayList<>();

        for (SearchSku sku : page) {
            ReturnSku returnSku = new ReturnSku();
            returnSku.setId(sku.getId());
            returnSku.setGoodsName(sku.getSkuName());
            returnSku.setPrice(sku.getPrice());
            returnSku.setMidlogo(sku.getLogo());
            returnSku.setCommentCount(sku.getCommentCount());
            list.add(returnSku);
        }

        //封装数据
        Map map1 = new HashMap<>();
        map1.put("count",count);
        map1.put("list",list);
        return map1;
    }

}
